<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/7/1
 * Time: 22:14
 */

namespace app\home\model;


use think\Db;

class NewsCommunicationDetail extends Base
{
    protected $insert = [
        'create_time' => NOW_TIME,
    ];

    /**
     * @param $id 上级文件id
     * @param $permission 权限
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDetail($id,$permission) {
        $detail = Db::table('zm_news_communication_detail')->alias('a')
            ->join('zm_news_communication b','a.communication_id = b.id','LEFT')
            ->field('a.id,a.userid,a.communication_id,a.remarks,a.status,b.title,b.type,b.content,b.attachment,
            b.create_time,b.operate_user,b.operate_status,b.operate_time,b.operate_remark')
            ->find(['a.id' => $id]);
        if(empty($detail)) {
            return false;
        }
        $type = [1 => "行政", 2 => "党建", 3 => "工会", 4 => "其它" ];
        $statusArr = [0 => "待审批", 1 => "已通过", 2 => "已驳回"];
        $detail['type_text'] = $type[$detail['type']];
        $detail['operate_status_text'] = isset($statusArr[$detail['operate_status']]) ? $statusArr[$detail['operate_status']] : "无";
        if($detail['attachment']) {
            $attachment = json_decode($detail['attachment']);
            $files = [];
            foreach ($attachment as $value) {
                $files[] = Db::table('zm_file')->where(['id' => $value])->find();
            }
            $detail['attachment'] = $files;
        }else {
            $detail['attachment'] = [];
        }
        $map = [
            'communication_id' => $detail['communication_id'],
        ];
        $list = Db::table("zm_news_communication_detail")->where($map)->select();
        foreach ($list as $k => $v) {
            $user = WechatUser::where(['userid' => $v['userid']])->find();
            $v['username'] = $user ? $user['name'] : "暂无用户";
            $detail['user'][$v['permission']][] = $v;
        };
        if($permission == 3 && $detail['status'] == 1) {
            $detail['is_file'] = 1; //归档
        }else {
            $detail['is_file'] = 0; //无权限
        }
        return $detail;
    }
}